//var MISSION_TYPE = {"ycd":2500,"zdb":3000,"ydb":4000,"ylj":7000,"zps":8000}
var MISSION_TYPE = {"ylj":7000,"zps":8000}
//var MISSION_TYPE_NAME = {"ycd":"待出单","zdb":"在打包","ydb":"已打包","ylj":"已领件","zps":"在派送"}
var MISSION_TYPE_NAME = {"ylj":"已领件","zps":"在派送"}
var COMPLEATE_ORDER_STATUS = "zps" ; //设置派送完成后跳转的路径
app.controller('MissionsCtrl', ['$scope','$state','$stateParams','appFactory','missionFactory','$cordovaBarcodeScanner',function($scope, $state,$stateParams,appFactory,missionFactory,$cordovaBarcodeScanner) {
    appFactory.ready().then(function(){
        $scope.initMissions();
    });
    $scope.doRefresh = function(){
        $scope.initMissions() ;
    };
    $scope.initMissions =function(){
        //替换getAllCountByStatus为getMissionCountFromOrder[2500,3000,4000,7000,8000]
        //var status = [2500,3000,4000,7000,8000]; //获取所有状态
        var status = [7000,8000]; //获取所有状态
        missionFactory.getMissionCountFromOrder(status).then(function(allcount){
            $scope.allMission = allcount;
            missionFactory.getCountByStatus(MISSION_TYPE,MISSION_TYPE_NAME).then(function(data){
                $scope.MissionsType = data.countByStatus;
                $scope.MissionsTypeCount = parseInt(data.countByStatus[0].count)+parseInt(data.countByStatus[1].count)
                $scope.MissionsTypeTHH = data.countByType;
            }).catch(function(){
                $scope.$broadcast('scroll.refreshComplete');
            })
        }).catch(function(){
            $scope.$broadcast('scroll.refreshComplete');
        }).finally(function(){
            $scope.$broadcast('scroll.refreshComplete');
        });
    };
    $scope.gotoMyMission = function(data){
        $state.go('tab.mymission',{status:MISSION_TYPE[data],type:"afljMISSIOIN",orderid:"",title:MISSION_TYPE_NAME[data]}) ;
    };
    //通过扫码进行查找，直接进入详情页
    $scope.scanZbra = function(){
        $cordovaBarcodeScanner
            .scan()
            .then(function(barcodeData) {
                var cancelled = barcodeData.cancelled;
                var result = JSON.parse(barcodeData.text);
                if(!cancelled){
                    var oid = result.OID;
                    var aid = result.AID ;
                    var shopid = result.WID ;
                    if(oid){
                        $state.go('tab.missionDetail',{orderid:oid}) ;
                    }else{
                        alert('没有获取到oid');
                    }
                }
            }, function(error) {
                alert("扫码失败，请手动输入");
            });
    };
    //
    $scope.searchMission = function(orderid){
        if(orderid.length<5){
            alert("请至少填写连续的5位数字");
            return ;
        }
        //替换searchMissions为searchMissionFormOrder
        missionFactory.searchMissionFormOrder(orderid).then(function(data){
            if(data.length >= 1){
                $state.go('tab.searchMission',{stauts:0,type:"mhcx",orderid:orderid,title:"我的任务"});
            }else{
                alert("没有找到要查询的订单");
            }
        });
    }
    var BTN_TYPE = {pond:'#',retrunGoods:'tab.returnGoodsMission',exchangeGoods:'tab.exchangeGoodsMission'};
    var ORDER_TYPE = {pond:'#',retrunGoods:11,exchangeGoods:12};
    var MISSION_TITLE1 = {pond:'#',retrunGoods:"退货任务",exchangeGoods:"换货任务"};
    $scope.getURL = function(data){
        $state.go(BTN_TYPE[data],{orderType:ORDER_TYPE[data],title:MISSION_TITLE1[data],typeName:data});
    }
}]);
app.controller('SearchMissionCtrl', ['$scope','$state','$stateParams','appFactory','missionFactory','$cordovaBarcodeScanner',function($scope, $state,$stateParams,appFactory,missionFactory,$cordovaBarcodeScanner) {

    var orderid = $stateParams.orderid;
    var uid = appFactory.getUserInfo().uid;
    var area = appFactory.globalArea();
    var shopArea = appFactory.shopArea();
    appFactory.ready().then(function(){
        $scope.missions();
    });
    $scope.doRefresh = function(){
        $scope.missions() ;
    };
    $scope.missions =function(){
        var arrayList = new Array();
        var goodsShop = '' ;
        missionFactory.searchMissionByHand(1,orderid).then(function(data){
            async.eachSeries(data,function(eachMission,callback){
                if([6,7,8].indexOf(eachMission.get('type')) >=0){
                    //外卖商品显示果饮店
                    goodsShop = shopArea[eachMission.get('shopId')+""]
                }else{
                    //普通商品，显示分区
                    goodsShop = area[eachMission.get('areaId').areaId+""]
                }
                arrayList.push({orderid:eachMission.get('orderid'),orderInfo:eachMission,address:eachMission.get('address'),goods:eachMission.get('goods'),area:area[eachMission.get('areaId')+""],shop:goodsShop,bookdate:eachMission.get('appointmentTime'),orderStatus:eachMission.get('status')});
                callback(null, arrayList);
            },function(err){
                if(err){
                    console.log(err);
                }else{
                    missionFactory.searchMissionByHandCount(orderid).then(function(res) {
                        if (arrayList.length === res) {
                            $scope.loadmore = {more: '没有更多了~'};
                        } else {
                            $scope.loadmore = {more: '加载更多~'};
                        }
                        $scope.$apply(function(){
                            $scope.mymissions=arrayList;
                            $scope.array = arrayList;
                            $scope.title =$stateParams.title;
                        });
                    });
                }
            })
        }).catch(function(){
            $scope.$broadcast('scroll.refreshComplete');
        }).finally(function(){
            $scope.$broadcast('scroll.refreshComplete');
        });
    };
    $scope.gotoDetail=function(orderid){
        //window.location.href='#/app/missionDetail/'+orderid;
        $state.go('tab.missionDetail',{orderid:orderid}) ;
    }
    $scope.goToFirst = function(orderid){
        //置顶功能：向数据库中更新时间，按时间降序排序
        //alert(orderid);
        missionFactory.updateMissionUpdatedAtOrder(orderid).then(function(data){
            if(data.code === 0){
                $scope.missions();
            }else{
                alert('sorry,失败了');
            }
        });
    };
    $scope.getMore = function(){
        missionFactory.searchMissionByHandCount(orderid).then(function(res){
            if($scope.array.length === res){
                $scope.loadmore = {more:'没有更多了~'};
            }else{
                $scope.page++;
                var goodsShop = '' ;
                missionFactory.searchMissionByHand($scope.page,orderid).then(function(data){
                    async.eachSeries(data,function(eachMission,callback){
                        if([6,7,8].indexOf(eachMission.get('type')) >=0){
                            //外卖商品显示果饮店
                            goodsShop = shopArea[eachMission.get('shopId')+""]
                        }else{
                            //普通商品，显示分区
                            goodsShop = area[eachMission.get('areaId').areaId+""]
                        }
                        $scope.array.push({orderid:eachMission.get('orderid'),orderInfo:eachMission,address:eachMission.get('address'),goods:eachMission.get('goods'),area:area[eachMission.get('areaId')+""],shop:goodsShop,bookdate:eachMission.get('appointmentTime'),orderStatus:eachMission.get('status')});
                        callback(null, $scope.array);
                    },function(){
                        $scope.$apply(function(){
                            $scope.mymissions = $scope.array;
                            $scope.loadmore = {more:'加载更多~'};
                        });
                    })
                })
            }
        })
    }
    $scope.dispatching=function(event,orderid,mission){
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        obj.disabled=true;
        //订单编号    orderId
        //变更状态   status === 8000
        missionFactory.dispatching(orderid).then(function(data){
            if(data.code === 0){
                $scope.mymissions.splice(mission,1);
                alert('已开始派送');
                console.log('正在派送')
            }else{
                console.log('出错了')
            }
        });

    };
    $scope.successfully = function(event,orderid,mission){
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        obj.disabled=true;
        var dialog = 0 ;
        if(!confirm("确认完成？")){
            return ;
        }
        missionFactory.completeDispatching(orderid,dialog,'直接完成').then(function(data){
            if(data.code===0){
                //取消成功，直接返回到任务首页？
                //$state.go('tab.missions');
                $scope.mymissions.splice(mission,1);
                alert('派送成功');
            }else{
                alert(data.msg);
                obj.disabled=false;
            }
        });
        /*var dialog = prompt('请填写实收金额');
        if(dialog){
            missionFactory.completeDispatching(orderid,dialog,'直接完成').then(function(data){
                if(data.code===0){
                    //取消成功，直接返回到任务首页？
                    //$state.go('tab.missions');
                    $scope.mymissions.splice(mission,1);
                    //alert('派送完成 ');
                }else{
                    alert(data.msg);
                }
            });
        }else{
            obj.disabled=false;
        }*/
    }
}]);
app.controller('MyMissionCtrl', ['$scope','$state','$stateParams','appFactory','missionFactory',function($scope, $state,$stateParams,appFactory,missionFactory) {
    var MISSION_TYPE_3 = 3 ;
    var searchOrder = $stateParams;
    appFactory.ready().then(function(){
        $scope.missions();
    });
    $scope.doRefresh = function(){
        $scope.missions() ;
    };
    $scope.page=1;
    $scope.limit=10;
    $scope.array = [];

    //var array = new Array();
    $scope.missions = function(){
        var uid = appFactory.getUserInfo().uid;
        var area = appFactory.globalArea();
        var shopArea = appFactory.shopArea();
        var arrayList = new Array;
        var goodsShop = '' ;
        missionFactory.getMissionInfoGroupByOrderStatus(1,searchOrder).then(function(data){
            async.eachSeries(data,function(eachMission,callback){
                if([6,7,8].indexOf(eachMission.get('type')) >=0){
                    //外卖商品显示果饮店
                    goodsShop = shopArea[eachMission.get('shopId')+""]
                }else{
                    //普通商品，显示分区
                    goodsShop = area[eachMission.get('areaId').areaId+""]
                }
                arrayList.push({orderid:eachMission.get('orderid'),orderInfo:eachMission,address:eachMission.get('address'),goods:eachMission.get('goods'),area:area[eachMission.get('areaId')+""],shop:goodsShop,bookdate:eachMission.get('appointmentTime'),orderStatus:eachMission.get('status')});
                callback(null, arrayList);
            },function(err){
                if(err){
                    console.log(err);
                }else{
                    var status = [] ;
                    status.push(parseInt(searchOrder.status));
                    missionFactory.getMissionCountFromOrder(status).then(function(res) {
                        if (arrayList.length === res) {
                            $scope.loadmore = {more: '没有更多了~'};
                        } else {
                            $scope.loadmore = {more: '加载更多~'};
                        }
                        $scope.$apply(function(){
                            $scope.mymissions=arrayList;
                            $scope.array = arrayList;
                            $scope.title =$stateParams.title;
                        });
                    });
                }
            })
        }).catch(function(err){
            console.log(err);
            console.log("err err er ");
            $scope.$broadcast('scroll.refreshComplete');
        }).finally(function(){
            //alert('refreshComplete');
            $scope.title =$stateParams.title;
            //alert($scope.title);
            $scope.$broadcast('scroll.refreshComplete');
        });
    };


    $scope.gotoDetail=function(orderid){
        //window.location.href='#/app/missionDetail/'+orderid;
        $state.go('tab.missionDetail',{orderid:orderid}) ;
    }

    $scope.goToFirst = function(orderid){
        //置顶功能：向数据库中更新时间，按时间降序排序
        //alert(orderid);

        missionFactory.updateMissionUpdatedAtOrder(orderid).then(function(data){
            if(data.code === 0){
                $scope.missions();
            }else{
                alert('sorry,失败了');
            }
        });
    };

    $scope.getMore = function(){
        var uid = appFactory.getUserInfo().uid;
        var area = appFactory.globalArea();
        var shopArea = appFactory.shopArea();
        var status = [] ;
        status.push(parseInt(searchOrder.status));
        missionFactory.getMissionCountFromOrder(status).then(function(res){
            if($scope.array.length === res){
                $scope.loadmore = {more:'没有更多了~'};
            }else{
                $scope.page++;
                var goodsShop = '' ;
                missionFactory.getMissionInfoGroupByOrderStatus($scope.page,searchOrder).then(function(data){
                    async.eachSeries(data,function(eachMission,callback){
                        if([6,7,8].indexOf(eachMission.get('type')) >=0){
                            //外卖商品显示果饮店
                            goodsShop = shopArea[eachMission.get('shopId')+""]
                        }else{
                            //普通商品，显示分区
                            goodsShop = area[eachMission.get('areaId').areaId+""]
                        }
                        $scope.array.push({orderid:eachMission.get('orderid'),orderInfo:eachMission,address:eachMission.get('address'),goods:eachMission.get('goods'),area:area[eachMission.get('areaId')+""],shop:goodsShop,bookdate:eachMission.get('appointmentTime'),orderStatus:eachMission.get('status')});
                        callback(null, $scope.array);
                    },function(){
                        $scope.$apply(function(){
                            $scope.mymissions = $scope.array;
                            $scope.loadmore = {more:'加载更多~'};
                        });
                    })
                })
            }
        })
    }

    $scope.cancelMission = function(oid){
        alert('该订单已取消，请联系客服');
    }

    /**
     * 开始派送
     * @param event
     */
    $scope.dispatching=function(event,orderid,mission){
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        obj.disabled=true;
        //订单编号    orderId
        //变更状态   status === 8000
        missionFactory.dispatching(orderid).then(function(data){
            if(data.code === 0){
                $scope.mymissions.splice(mission,1);
                alert('已开始派送');
                console.log('正在派送')
            }else{
                console.log('出错了')
            }
        });

    };
    $scope.successfully = function(event,orderid,mission){
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        obj.disabled=true;
        if(!confirm("确认完成？")){
            return ;
        }
        var dialog = 0;
        missionFactory.completeDispatching(orderid,dialog,'直接完成').then(function(data){
            if(data.code===0){
                //取消成功，直接返回到任务首页？
                //$state.go('tab.missions');
                $scope.mymissions.splice(mission,1);
                //alert('派送完成 ');
            }else{
                alert(data.msg);
            }
        });
        /*var dialog = prompt('请填写实收金额');
        if(dialog){
            missionFactory.completeDispatching(orderid,dialog,'直接完成').then(function(data){
                if(data.code===0){
                    //取消成功，直接返回到任务首页？
                    //$state.go('tab.missions');
                    $scope.mymissions.splice(mission,1);
                    //alert('派送完成 ');
                }else{
                    alert(data.msg);
                }
            });
        }else{
            obj.disabled=false;
        }*/
    }

}]);

app.controller('MissionDetailCtrl', ['$scope','$stateParams','$state','appFactory','$cordovaGeolocation','$ionicActionSheet','$timeout','missionFactory',function($scope, $stateParams,$state,appFactory,$cordovaGeolocation, $ionicActionSheet,$timeout,missionFactory) {
    appFactory.ready().then(function(){
        $scope.missions();
    });
    var orderId = $stateParams.orderid;
    var missionM = AV.Object.extend("Mission");
    var orderM = AV.Object.extend("Order");
    var missionFlowM = AV.Object.extend("MissionFlow");
    var uid = appFactory.getUserInfo().uid;
    var area = appFactory.globalArea();
    var shopArea = appFactory.shopArea();
    var loadSomeOrders = function(){
        $scope.missions() ;
    };
    $scope.doRefresh = function(){
        loadSomeOrders();
    }
    $scope.inDispatching = 0 ;
    $scope.dispatchingSuccessful = 0 ;
    $scope.missions = function(){
        missionFactory.showMissionDetails(orderId).then(function(data){
            if(data.code === 0){
                $scope.inDispatching = data.button_show.inDispatching;
                $scope.dispatchingSuccessful = data.button_show.dispatchingSuccessful;
                $scope.mymissions=data.data;
            }else{
                $scope.mymissions={};
            }
        }).catch(function(){
            alert('sorry，出错了');
        }).finally(function(){
            $scope.$broadcast('scroll.refreshComplete');
        });
    };
    //$scope.missions();
    //开始配送，更改状态和发短信
    $scope.dispatching=function(event){
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        obj.disabled=true;
        //订单编号    orderId
        //变更状态   status === 8000
        $scope.inDispatching = 0;
        $scope.dispatchingSuccessful = 1;
        missionFactory.dispatching(orderId).then(function(data){
            if(data.code === 0){
                $scope.inDispatching = 0;
                $scope.dispatchingSuccessful = 1;
            }else{
                $scope.inDispatching =1;
                $scope.dispatchingSuccessful = 0;
            }
        });

    };
    //短信测试：
    $scope.sendMessage=function(){
        var uid = appFactory.getUserInfo().uid;
        var area = appFactory.globalArea();
        var shopArea = appFactory.shopArea();
        async.auto({
            //获取order信息
            getOrderInfo:function(callback){
                var queryMission = new AV.Query(missionM);
                queryMission.equalTo('uid',uid+"");
                queryMission.equalTo('status',1);
                queryMission.equalTo('orderid',orderId);
                queryMission.first().then(function(data){
                    callback(null, data);
                }).catch(function(err){
                    callback(err);
                });
            },

            getAddress:["getOrderInfo",function(callback,results){
                //根据addrId获取地址信息
                console.log(results.getOrderInfo);

                if(results.getOrderInfo){
                    var orderInfo = results.getOrderInfo.get('detail');
                    callback(null, {orderid:orderId,orderInfo:orderInfo,address:orderInfo.address,goods:orderInfo.goods,area:area[orderInfo.areaId]});
                }else{
                    callback(null, {orderid:"",orderInfo:{},address:{},goods:{},area:{}});
                }
            }],

            final:['getAddress',function(callback,results){
                var phone = results.getAddress.address.phone;
                var uName = results.getAddress.address.name;
                console.log(uName);
                AV.Cloud.requestSmsCode({
                    mobilePhoneNumber: phone,
                    template: "配送通知",
                    orderid:orderId,
                    name: '果然100',
                    uName:uName
                }).then(function(data){
                    //发送成功
                    callback(null, {code:0,msg:'发送成功'});
                }, function(err){
                    //发送失败
                    console.log(err);
                    callback(null, {code:1,msg:'发送失败'});
                });
                //callback(null, {code:0,msg:'发送成功'});
            }]
        },function(err,results){
            if(err){
                console.log(err);
            }else{
                console.log(results.final);
            }
        });
    }
    //配送成功
    $scope.successfully = function(payway,total){

        //window.location.href='#/app/completMission/'+orderId+'/'+payway+'/'+total;
        $state.go('tab.completMission',{orderid:orderId,payway:payway,finalAmount:total});
    }

    $scope.Sheet = function() {
        var btnArr=[];

        var hideSheet = $ionicActionSheet.show({
            buttons:[{text:''},{text:'确认'}],
            titleText: '推迟派送',
            cancelText: '取消',
            cancel: function(){
            },
            buttonClicked: function(index) {
                hideSheet();
            }
        });
        $timeout(function() {
            hideSheet();
        }, 5000);
    };
    $scope.appointment = function(){
        //预约时间
        if($scope.showAppointment == 1){
            $scope.showAppointment = 0;
        }else{
            $scope.showAppointment = 1;
        }
    }
    $scope.sureBookdate = function(){
        var yydate = new Date($scope.mymissions.yydate);
        var yyHour = $scope.mymissions.yytime;
        var bookdate = new Date(new Date(yydate).toDateString() +' '+yyHour+':00:00');
        if(bookdate < new Date()){
            alert('亲，时间不能穿越哦');
            return ;
        }
        //将预约时间更新到mission表中
        var info = {appointmentTime:bookdate}
        missionFactory.updateMissionInfo(orderId,info).then(function(res){
            if(res.code === 0){
                alert('重新预约配送时间成功');
            }else{
                alert('预约失败或者订单不可以预约');
            }
            $scope.showAppointment = 0;
        }).catch(function(err){
            alert('预约失败或者订单不可以预约');
            $scope.showAppointment = 0;
        }).finally(function(){
            $scope.mymissions.bookdate = bookdate;
        });
    }
    //取消派送，填写取消原因，并修改订单状态status=7000，保存取消原因cancelReason和取消时间canceledAt
    $scope.cancel = function(event){
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        /*var type = [11,12] ;
        console.log(type.indexOf(11));
        if(type.indexOf(11)>=0){
            console.log(type.indexOf(11));
            alert(111);
        }*/
        //obj.disabled=true;
        var dialog = prompt('请填写取消原因');
             if(dialog){
                 missionFactory.cancelDispatching(orderId,dialog).then(function(data){
                     if(data.code===0){
                         //取消成功，直接返回到任务首页？
                         $state.go('tab.missions');
                     }else{
                         alert(data.msg);
                     }
                 });
             }else{
                 obj.disabled=false;
         }
    }
}]);
app.controller('CompletMissionCtrl',['$scope','$stateParams','$state','appFactory','missionFactory',function($scope, $stateParams,$state,appFactory,missionFactory) {
    var orderId = $stateParams.orderid;
    var payWay = $stateParams.payway;   //支付方式
    var finalAmount = $stateParams.finalAmount;      //总金额
    var loadSomeOrders = function () {
        $scope.init();
    };
    $scope.doRefresh = function () {
        loadSomeOrders();
    }
    var uid = appFactory.getUserInfo().uid;
    var missionM = AV.Object.extend("Mission");
    var orderM = AV.Object.extend("Order");
    var missionFlowM = AV.Object.extend("MissionFlow");
    var Order = AV.Object.extend("Order");
    $scope.init = function () {
        var queryOrder = new AV.Query(Order);
        queryOrder.equalTo('orderid', orderId);
        queryOrder.first().then(function(data){
            if (data) {
                if (data.get('status') != 8000) {
                    $scope.complete = 0;
                } else {
                    $scope.complete = 1;
                }
                //alert('更新成功，没有数据'+data.get('detail').status);
                $scope.$broadcast('scroll.refreshComplete');
            } else {
                $scope.complete = 0;
                //alert('更新成功');
                $scope.$broadcast('scroll.refreshComplete');
            }
        }).catch(function(err){
            console.log(err);
            alert('没有找到订单');
        });

    };
    $scope.init();

    //根据orderId查询出order详细，获取支付方式和支付金额。核对验证
    $scope.completMision = {payway: '', finalAmount: '', hideRealTotal: '', remark: '', realtotal: ''};
    $scope.completMision.payway = payWay;
    if ($scope.completMision.payway === '货到付款') {
        $scope.completMision.finalAmount = finalAmount;
        $scope.completMision.hideRealTotal = false;
    } else {
        $scope.completMision.finalAmount = 0;
        $scope.completMision.hideRealTotal = true;
    }
    $scope.completMision.remark = '';
    console.log($scope.completMision.realtotal);
    //保存完成后需要将订单状态更新到order表和mission表
    //配送成功
    $scope.save = function(event){
        event = event ? event : window.event;
        var obj = event.srcElement ? event.srcElement : event.target;
        obj.disabled=true;
        var complete = 0 ;
        var realtotal = 0;
        var remark = $scope.completMision.remark;
        if($scope.completMision.hideRealTotal === true){
            //微信支付
            realtotal = 0;
            complete = 1 ;
        }else{
            if (!$scope.completMision.realtotal) {
                realtotal = $scope.completMision.finalAmount;
            } else {
                realtotal = $scope.completMision.realtotal;
            }
            if($scope.completMision.finalAmount === realtotal ){
                if(confirm('确认派送成功？') == true){
                    complete = 1 ;
                }else{
                    complete = 0 ;
                    return ;
                }
            }else{
                if($scope.completMision.remark != ''){
                    if(confirm('应收款和实际收款不等，确认派送成功？') == true){
                        complete = 1 ;
                    }else{
                        complete = 0 ;
                        return ;
                    }
                }else{
                    alert('请填写备注!');
                    complete = 0 ;
                    obj.disabled=false;
                    return ;
                }
            }
        }

        if(complete === 1){
            missionFactory.completeDispatching(orderId,realtotal,remark).then(function(data){
                if(data.code === 0){
                    //派送成功
                    alert('派送成功');
                }else{
                    //派送出错了
                    alert('派送出错'+data.msg);
                }
            }).catch(function(err){
                //派送出错了
                alert('派送出错'+err);
            }).finally(function(){
                $state.go('tab.missions');
            });
        }
    };
    $scope.alipay = function () {
        var $modal = angular.element("#alipay");
        //progress.start();
        $modal.modal();
    };
    var remarkText = {
        cancel:'用户取消订单',
        daofu:'到付',
        yifu:'已付',
        other:'其他'
    };
    $scope.remarkQuickly = function(args){
        $scope.completMision.remark = remarkText[args];
    }
}]);
